package com.coderjacky.product.service.impl;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.coderjacky.product.domain.PmsSkuImages;
import com.coderjacky.product.domain.PmsSpuInfoDesc;
import com.coderjacky.product.mapper.*;
import com.coderjacky.product.vo.AttrGroupVo;
import com.coderjacky.product.vo.ItemInfoVo;
import com.coderjacky.product.vo.SaleAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.coderjacky.product.domain.PmsSkuInfo;
import com.coderjacky.product.service.IPmsSkuInfoService;

/**
 * sku信息Service业务层处理
 *
 * @author ccy
 * @date 2024-01-16
 */
@Service
public class PmsSkuInfoServiceImpl implements IPmsSkuInfoService
{
    @Autowired
    private PmsSkuInfoMapper pmsSkuInfoMapper;


    @Autowired
    private PmsSkuImagesMapper skuImagesMapper;


    @Autowired
    private PmsSpuInfoDescMapper spuInfoDescMapper;


    @Autowired
    private PmsSkuSaleAttrValueMapper skuSaleAttrValueMapper;


    @Autowired
    private PmsAttrGroupMapper attrGroupMapper;


    @Autowired
    private ThreadPoolExecutor itemThreadPoolExcuter;

    /**
     * 查询sku信息
     *
     * @param skuId sku信息主键
     * @return sku信息
     */
    @Override
    public PmsSkuInfo selectPmsSkuInfoBySkuId(Long skuId)
    {
        return pmsSkuInfoMapper.selectPmsSkuInfoBySkuId(skuId);
    }

    /**
     * 查询sku信息列表
     *
     * @param pmsSkuInfo sku信息
     * @return sku信息
     */
    @Override
    public List<PmsSkuInfo> selectPmsSkuInfoList(PmsSkuInfo pmsSkuInfo)
    {
        return pmsSkuInfoMapper.selectPmsSkuInfoList(pmsSkuInfo);
    }

    /**
     * 新增sku信息
     *
     * @param pmsSkuInfo sku信息
     * @return 结果
     */
    @Override
    public int insertPmsSkuInfo(PmsSkuInfo pmsSkuInfo)
    {
        return pmsSkuInfoMapper.insertPmsSkuInfo(pmsSkuInfo);
    }

    /**
     * 修改sku信息
     *
     * @param pmsSkuInfo sku信息
     * @return 结果
     */
    @Override
    public int updatePmsSkuInfo(PmsSkuInfo pmsSkuInfo)
    {
        return pmsSkuInfoMapper.updatePmsSkuInfo(pmsSkuInfo);
    }

    /**
     * 批量删除sku信息
     *
     * @param skuIds 需要删除的sku信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSkuInfoBySkuIds(Long[] skuIds)
    {
        return pmsSkuInfoMapper.deletePmsSkuInfoBySkuIds(skuIds);
    }

    /**
     * 删除sku信息信息
     *
     * @param skuId sku信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSkuInfoBySkuId(Long skuId)
    {
        return pmsSkuInfoMapper.deletePmsSkuInfoBySkuId(skuId);
    }

    @Override
    public ItemInfoVo selectSkuItemInfoBySkuId(Long skuId) throws ExecutionException, InterruptedException {
        ItemInfoVo itemInfoVo = new ItemInfoVo();


//        1.查sku详情   0.5s
        CompletableFuture<PmsSkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            PmsSkuInfo pmsSkuInfo = this.selectPmsSkuInfoBySkuId(skuId);
            itemInfoVo.setSkuInfo(pmsSkuInfo);
            return pmsSkuInfo;
        },itemThreadPoolExcuter);

//
//        2.查sku图片信息 1s

        CompletableFuture<Void> skuImageCompletableFuture = CompletableFuture.runAsync(() -> {
            PmsSkuImages skuImages = new PmsSkuImages();
            skuImages.setSkuId(skuId);
            List<PmsSkuImages> pmsSkuImages = skuImagesMapper.selectPmsSkuImagesList(skuImages);
            itemInfoVo.setSkuImagesList(pmsSkuImages);
        },itemThreadPoolExcuter);

//
//        3.查spu相关信息销售属性 1s
        CompletableFuture<Void> skuSaleAttrValueCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            List<SaleAttrVo> saleAttrVos = skuSaleAttrValueMapper.selectSkuSaleAttrs(skuInfo.getSpuId());
            itemInfoVo.setSaleAttrs(saleAttrVos);
        },itemThreadPoolExcuter);


//
//        4.查spu商品介绍 1s

        CompletableFuture<Void> spuInfoDescCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            PmsSpuInfoDesc pmsSpuInfoDesc = spuInfoDescMapper.selectPmsSpuInfoDescBySpuId(skuInfo.getSpuId());
            itemInfoVo.setSpuInfoDesc(pmsSpuInfoDesc);
        },itemThreadPoolExcuter);



//
//        5.查规格和包装 05s

        CompletableFuture<Void> attrGroupCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            List<AttrGroupVo> attrGroupVos = attrGroupMapper.selectAttrGroupVos(skuInfo.getSpuId());
            itemInfoVo.setAttrGroupVos(attrGroupVos);
        },itemThreadPoolExcuter);


        CompletableFuture.allOf(skuImageCompletableFuture,skuSaleAttrValueCompletableFuture,attrGroupCompletableFuture,spuInfoDescCompletableFuture).get();

        return itemInfoVo;
    }

    /**
     * 指定线程池，执行异步任务
     */
    @Async("itemThreadPoolExcuter")
    public void asyncMethod(){

        System.out.println("异步执行");
    }
}
